package com.qst.rongxiaotong.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.rongxiaotong.mapper.OrderMapper;
import com.qst.rongxiaotong.context.BaseContext;
import com.qst.rongxiaotong.exception.BaseException;
import com.qst.rongxiaotong.exception.MessageConstant;
import com.qst.rongxiaotong.pojo.MyPurchase;
import com.qst.rongxiaotong.pojo.Order;
import com.qst.rongxiaotong.pojo.User;
import com.qst.rongxiaotong.service.OrderService;
import com.qst.rongxiaotong.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Ww
 * @version v1.0
 * @className OrderServiceiMPL
 * @description
 * @date 2024/10/28 9:55
 **/
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    OrderMapper orderMapper;
    Integer pageSize=10;

    @Autowired
    UserService userService;
    @Override
    public PageInfo<Order> selectAll(Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);

        List<Order> goods = orderMapper.selectAll();
        PageInfo<Order> pageInfo=new PageInfo<>(goods);


        return pageInfo;
    }

    @Override
    public PageInfo<Order> selectAllGoods(Integer pageNum) {
        Order order = new Order();
        order.setType("goods");
        PageHelper.startPage(pageNum, pageSize);
        List<Order> goods = orderMapper.selectAllGoods(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(goods);
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> selectAllByKeys(String keys, Integer pageNum) {
        Order order = new Order();
        order.setContent(keys);
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orders = orderMapper.selectByKeys(order);
//        List<Order> goods = orderMapper.selectAllGoods(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        return orderPageInfo;
    }

    @Override
    public PageInfo<Order> selectGoodsByKeys(String keys, Integer pageNum) {
        // 创建 Order 实例
        Order order = new Order();
        order.setType("goods");
        order.setContent(keys);

        // 获取用户信息
        User user = userService.finduser();

        try {
                if (!user.getRole().equals("admin")) {
                    order.setOwnName(user.getUserName()); // 假设 User 有 getName() 方法返回用户名
                    order.setOrderStatus(0);
                }
        } catch (Exception e) {
            throw new BaseException(MessageConstant.USER_NOT_ROLE);
        }

        // 分页
        PageHelper.startPage(pageNum, pageSize); // pageSize 可以设置成一个固定值或作为方法参数传入
        // 查询
        List<Order> orders = orderMapper.selectByKeys(order);
        System.out.println(orders);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;
    }

    @Override
    public Order selectById(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        return order;
    }

    @Override
    public PageInfo<Order> selectByType(String type, Integer pageNum) {
        Order order = new Order();
        order.setType(type);
        // 获取用户信息
        User user = userService.finduser();

        try {
            if (!user.getRole().equals("admin")) {
                order.setOwnName(user.getUserName()); // 假设 User 有 getName() 方法返回用户名
                order.setOrderStatus(0);
            }
        } catch (Exception e) {
            throw new BaseException(MessageConstant.USER_NOT_ROLE);
        }
        // 分页
        PageHelper.startPage(pageNum, pageSize); // pageSize 可以设置成一个固定值或作为方法参数传入
        // 查询
        List<Order> orders = orderMapper.selectByKeys(order);
        System.out.println(orders);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        return orderPageInfo;


    }

    @Override
    public void add(Order order) {
        User user = userService.finduser();
        if(!StringUtils.isEmpty(user.getAddress())){
            order.setAddress(user.getAddress());
        }
        order.setOwnName(user.getUserName());
        orderMapper.insertSelective(order);
    }

    @Override
    public void update(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void delete(Integer id) {
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Order> selectAllNeeds(Integer pageNum) {
        Order order = new Order();
        order.setType("needs");

        // 获取用户信息
        User user = userService.finduser();

        try {
            if (!user.getRole().equals("admin")) {
                order.setOwnName(user.getUserName()); // 假设 User 有 getName() 方法返回用户名
                order.setOrderStatus(0);
            }
        } catch (Exception e) {
            throw new BaseException(MessageConstant.USER_NOT_ROLE);
        }
        // 分页
        PageHelper.startPage(pageNum, pageSize); // pageSize 可以设置成一个固定值或作为方法参数传入
        // 查询
        List<Order> orders = orderMapper.selectByKeys(order);
        System.out.println(orders);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        return orderPageInfo;
    }
    @Override
    public PageInfo<Order> selectNeedsByKeys(String keys, Integer pageNum) {
        //创建Order实例
        Order order = new Order();
        order.setType("needs");
        order.setContent(keys);

        //分页
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List<Order> orders = orderMapper.selectByKeys(order);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        return orderPageInfo;
    }

    @Override
    public List<MyPurchase> selectBuys() {
        String currentuserName = BaseContext.getCurrentuserName();
        List<MyPurchase> myPurchaseList = orderMapper.selectBuys(currentuserName);
        return myPurchaseList;
    }

}
